package com.ihr360.insurance.controller.benefit;

import com.ihr360.authority.sharing.annonation.ControlMode;
import com.ihr360.authority.sharing.annonation.RestControl;
import com.ihr360.authority.sharing.privilege.AccessControlMethod;
import com.ihr360.commons.model.type.BenefitCategory;
import com.ihr360.commons.vo.PageData;
import com.ihr360.insurance.entity.request.SingleStaffBenefitVoRequest;
import com.ihr360.insurance.entity.request.StaffBenefitRequest;
import com.ihr360.insurance.entity.request.StaffBenefitVoRequest;
import com.ihr360.insurance.entity.request.StaffIdRequest;
import com.ihr360.insurance.entity.vo.CompanyBenefitSimpleVo;
import com.ihr360.insurance.entity.vo.StaffBenefitVo;
import com.ihr360.insurance.model.dto.benefits.CompanyBenefit;
import com.ihr360.insurance.service.CompanyBenefitService;
import com.ihr360.insurance.service.StaffBenefitService;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 社保公积金方案
 *
 * @author david.wei
 */
@RestController
@RequestMapping("/companyBenefits")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.MANDATORY_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.MANDATORY_NAME,
        hasExtendsAbstractRestController = true)
@RestControl(mode = ControlMode.ALLOW,
        controls = {AccessControlMethod.METHODS.deleteOne,
                AccessControlMethod.METHODS.batchDelete,
                AccessControlMethod.METHODS.getOne,
                AccessControlMethod.METHODS.update,
                AccessControlMethod.METHODS.search})
public class CompanyBenefitController extends AbstractRestSimpleWithLongIdController<CompanyBenefit> {

    @Autowired
    private StaffBenefitService staffBenefitService;

    @Autowired
    CompanyBenefitService companyBenefitService;

    @Override
    public SimpleRestService getSimpleRestService() {
        return companyBenefitService;
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "已关联社保公积金方案的人数", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/staff/count/related", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public int getRelatedStaffCount(@RequestParam String companyId,
                                    @RequestParam BenefitCategory category,
                                    @RequestBody StaffIdRequest staffIdRequest) {
        return staffBenefitService.getRelatedStaffCount(companyId, category, staffIdRequest.getStaffIds());
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "未关联社保公积金方案的在职人数", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/staff/count/unRelated", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public int getUnRelatedStaffCount(@RequestParam String companyId,
                                      @RequestParam BenefitCategory category,
                                      @RequestBody StaffIdRequest staffIdRequest) {
        return staffBenefitService.getUnRelatedStaffCount(companyId, category, staffIdRequest.getStaffIds());
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "保存员工的社保公积金相关数据", functionCode = ModuleCodeStore.Function.UPDATE)
    @RequestMapping(value = "/staff/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public void save(@RequestParam String companyId,
                     @RequestParam BenefitCategory category,
                     @RequestBody StaffBenefitVoRequest request) {
        staffBenefitService.saveBenefits(companyId, request.getBenefitVos(), category, request.getStaffIds());
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "更新员工的社保或者公积金数据", functionCode = ModuleCodeStore.Function.UPDATE)
    @RequestMapping(value = "/staff/saveSiOrHf", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public void updateStaffBenefit(@RequestParam String companyId,
                                   @RequestParam BenefitCategory category,
                                   @RequestBody SingleStaffBenefitVoRequest request) {
        staffBenefitService.saveSingleBenefits(companyId, request.getSingleBenefitVos(), category, request.getStaffIds());
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "将员工从社保或者公积金方案中移除", functionCode = ModuleCodeStore.Function.DELETE)
    @RequestMapping(value = "/staff/deleteSiOrHf", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public void deleteStaffBenefit(@RequestParam String companyId,
                                   @RequestParam BenefitCategory category,
                                   @RequestBody SingleStaffBenefitVoRequest request) {
        staffBenefitService.deleteSingleBenefits(companyId, request.getSingleBenefitVos(), category, request.getStaffIds());
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "获取指定id范围内的、未关联社保/公积金方案的员工数据", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/getUnrelatedList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public PageData<StaffBenefitVo> getUnrelated(@RequestParam String companyId,
                                                 @RequestBody StaffBenefitRequest req) {
        return staffBenefitService.getUnrelatedStaffs(companyId, req.getStaffIds(), req);
    }

    @FunctionDefinition(functionName = "保存社保公积金方案", functionCode = ModuleCodeStore.Function.UPDATE)
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public Long save(@RequestParam String companyId, @RequestBody CompanyBenefit req) {
        return companyBenefitService.saveBenefit(companyId, req);
    }

    @FunctionDefinition(functionName = "复制社保公积金方案", functionCode = ModuleCodeStore.Function.ADD)
    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean copy(@RequestParam String companyBenefitName, @RequestParam Long companyBenefitId) {
        return companyBenefitService.copyBenefit(companyBenefitName, companyBenefitId);
    }

    @FunctionDefinition(functionName = "获取社保公积金方案列表", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/list/simple", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public List<CompanyBenefitSimpleVo> getSimpleCompanyBenefits(@RequestParam String companyId) {
        return companyBenefitService.getCompanyBenefitVos(companyId);
    }

    @FunctionDefinition(functionName = "获取社保公积金方案中包含缴纳类型", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/mode", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<Long, String> getCompanyBenefitModes(@RequestParam String companyId,
                                                    @RequestParam BenefitCategory category) {
        return companyBenefitService.getCompanyBenefitModes(companyId, category);
    }
}